package com.ysyx.ysyx_driverinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ysyx.ysyx_driverinfo.dto.BaseDto;
import com.ysyx.ysyx_driverinfo.mapper.CarPojoMapper;
import com.ysyx.ysyx_driverinfo.pojo.CarPojo;
import com.ysyx.ysyx_driverinfo.pojo.DriverPojo;
import com.ysyx.ysyx_driverinfo.qo.DriverQo;
import com.ysyx.ysyx_driverinfo.qo.GeoQo;
import com.ysyx.ysyx_driverinfo.qo.TakeOrderQo;
import com.ysyx.ysyx_driverinfo.service.DriverPojoService;
import com.ysyx.ysyx_driverinfo.mapper.DriverPojoMapper;
import com.ysyx.ysyx_driverinfo.vo.DriverdetailVo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class DriverPojoServiceImpl implements DriverPojoService{

    @Autowired
    private DriverPojoMapper driverPojoMapper;

    @Autowired
    private CarPojoMapper carPojoMapper;

    @Autowired
    private RedissonClient redissonClient;

    /*
     * @Description: (作用) 司机账号登录
     * @params: String driverAcc,String driverPwd
     * @return: statusCode 0程序错误 101登录成功,driverPojo 201账号不存在 301账号或密码错误
     * @Author: 郭宇轩
     * @create:2023/4/8
     */
    @Override
    public BaseDto login(String driverAcc, String driverPwd) {

        System.out.println("driver="+driverAcc+","+driverPwd);

        int resultStatus = 0;
        BaseDto baseDto = new BaseDto();

        QueryWrapper<DriverPojo> qw = new QueryWrapper<>();
        qw.eq("driver_acc",driverAcc);
        int resultAcc = driverPojoMapper.selectCount(qw);
        if(resultAcc == 0){
            resultStatus = 201;
        }else{
            qw.eq("driver_pwd",driverPwd);
            List<DriverPojo> driverPojos = driverPojoMapper.selectList(qw);
            if(driverPojos.size() == 1 ){

                QueryWrapper<CarPojo> carQw = new QueryWrapper<>();
                carQw.eq("driver_id",driverPojos.get(0).getDriverId());
                CarPojo carPojo = carPojoMapper.selectOne(carQw);
                if(carPojo != null){
                    resultStatus = 101;
                }else{
                    resultStatus = 102;
                }

                baseDto.setList(driverPojos);
                //成功登录将其存在redis中
                DriverPojo driverPojo = driverPojos.get(0);
                RMap<Object, Object> map = redissonClient.getMap("driverinfo:" + driverPojos.get(0).getDriverId());
                map.put("driverId",driverPojo.getDriverId());
                map.put("driverAcc",driverPojo.getDriverAcc());
                map.put("driverPwd",driverPojo.getDriverPwd());
                map.put("driverName",driverPojo.getDriverName());
                map.put("driverIdno",driverPojo.getDriverIdno());
                map.put("driverTel",driverPojo.getDriverTel());
                map.put("driverAvatar",driverPojo.getDriverAvatar());
                map.put("driverIdnoimg",driverPojo.getDriverIdnoimg());
                map.put("driverCtime",driverPojo.getDriverCtime());
                map.put("driverPassstate",driverPojo.getDriverPassstate());
                map.put("driverDriverstate",driverPojo.getDriverState());
                map.put("driverWallet",driverPojo.getDriverWallet());
                //再去拿车辆信息
                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("driver_id",driverPojo.getDriverId());
                List list = carPojoMapper.selectList(queryWrapper);
                if (list.size()==0){
                    resultStatus = 202;
                }else {
//                    CarPojo carPojo = (CarPojo) list.get(0);
                    map.put("carPlateno",carPojo.getCarPlateno());
                    map.put("carColor",carPojo.getCarColor());
                    map.put("carBrand",carPojo.getCarBrand());
                }


            }else{
                resultStatus = 301;
            }
        }
        baseDto.setStatusCode(resultStatus);

        return baseDto;
    }

    @Deprecated
    @Override
    public List<DriverPojo> selectAll() {
        QueryWrapper<DriverPojo> qw = new QueryWrapper<>();
        List<DriverPojo> pojoList = driverPojoMapper.selectList(qw);
        return pojoList;
    }

    @Override
    public BaseDto driverUpwork(GeoQo qo) {
        System.out.println(qo);

        RBucket<Object> bucket = redissonClient.getBucket("work:driver:" + qo.getDriverId());
        bucket.set(qo.getDriverId());

        //司机上工获取地理信息
        String areaName = "";

        List<RMap<Object,Object>> maps = new ArrayList<>();


        RMap<Object, Object> mapA1 = redissonClient.getMap("area:A1");
        RMap<Object, Object> mapA2 = redissonClient.getMap("area:A2");
        RMap<Object, Object> mapA3 = redissonClient.getMap("area:A3");
        RMap<Object, Object> mapB1 = redissonClient.getMap("area:B1");
        RMap<Object, Object> mapB2 = redissonClient.getMap("area:B2");
        RMap<Object, Object> mapB3 = redissonClient.getMap("area:B3");
        maps.add(mapA1);
        maps.add(mapA2);
        maps.add(mapA3);
        maps.add(mapB1);
        maps.add(mapB2);
        maps.add(mapB3);

        //判断司机的位置
        for (RMap<Object, Object> map : maps) {

            Double leftupLongitude = Double.parseDouble((String) map.get("leftupLongitude")) ;
            Double leftupLatitude = Double.parseDouble((String) map.get("leftupLatitude")) ;
            Double rightdownLongitude = Double.parseDouble((String) map.get("rightdownLongitude")) ;
            Double rightdownLatitude = Double.parseDouble((String) map.get("rightdownLatitude")) ;
            if (qo.getLongitude()>=leftupLongitude &&   //司机位置经度 >= 区域左上经度
                    qo.getLongitude()<=rightdownLongitude &&  //司机位置经度 <= 区域右下经度
                    qo.getLatitude()>=rightdownLatitude &&// 司机位置纬度 >= 区域右下纬度
                    qo.getLatitude()<=leftupLatitude// 司机位置纬度 >= 区域左上纬度
            )
            {
                areaName =  map.getName();
                break;
            }else {
                continue;
            }


        }
        //获取到司机所在地区的名称 areaname

        RGeo<String> geo = redissonClient.getGeo(areaName+":drivers");
        geo.add(new GeoEntry(qo.getLongitude(),qo.getLatitude(),qo.getDriverId()));

        return new BaseDto(101,"司机成功上工目前区域"+areaName,null);
    }

    @Override
    public BaseDto getDriverInfo(Integer driverId) {
        DriverdetailVo driverdetailVo = driverPojoMapper.selectDriverDetail(driverId);
        List<DriverdetailVo> driverdetailVos = new ArrayList<>();
        driverdetailVos.add(driverdetailVo);

        return new BaseDto(0,null,driverdetailVos);
    }

    @Override
    public BaseDto selectAllDriverDetail() {

        List<DriverdetailVo> driverdetailVos = driverPojoMapper.selectAllDriverDetail();

        return new BaseDto(0,null,driverdetailVos);
    }

    @Override
    public BaseDto selectAllOrder(Integer driverId) {
        List<TakeOrderQo> takeOrderQos = driverPojoMapper.selectAllTakeOrderQoByDriverId(driverId);
        return new BaseDto(0,null,takeOrderQos);
    }

    @Override
    public BaseDto DriverReg(DriverQo driverPojo) {
        System.err.println(driverPojo);

        QueryWrapper<DriverPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_acc",driverPojo.getDriverAcc());
        List<DriverPojo> driverPojos = driverPojoMapper.selectList(queryWrapper);
        if (driverPojos.size()>0){
            //存在相同用户名账号的β
            return  new BaseDto(201,"存在相同的账号",null);
        }

        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("driver_tel",driverPojo.getDriverPwd());
        List<DriverPojo> list = driverPojoMapper.selectList(queryWrapper1);
        if (list.size()>0) {
            return new BaseDto(202,"手机号已被绑定",null);
        }

        QueryWrapper<DriverPojo> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("driver_name",driverPojo.getDriverName());
        List<DriverPojo> driverPojos1 = driverPojoMapper.selectList(queryWrapper2);
        if (driverPojos1.size()>0) {
            return new BaseDto(203,"司机的名称重复",null);
        }

        QueryWrapper<DriverPojo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("driver_idno",driverPojo.getDriverIdno());
        List<DriverPojo> driverPojos2 = driverPojoMapper.selectList(queryWrapper3);
        if (driverPojos2.size()>0) {
            return new BaseDto(205,"司机身份证已被使用",null);
        }


        DriverPojo mysqlDriver = new DriverPojo();
        mysqlDriver.setDriverAcc(driverPojo.getDriverAcc());
        mysqlDriver.setDriverPwd(driverPojo.getDriverPwd());
        mysqlDriver.setDriverName(driverPojo.getDriverName());
        mysqlDriver.setDriverTel(driverPojo.getDriverTel());
//        mysqlDriver.setDriverAvatar("avatar"+driverPojo.getDriverAvatar()+".png");
        mysqlDriver.setDriverIdno(driverPojo.getDriverIdno());
        mysqlDriver.setDriverWallet(0.00);
        mysqlDriver.setDriverIdnoimg("idno"+driverPojo.getDriverIdno());
        mysqlDriver.setDriverState(201);
        mysqlDriver.setDriverPassstate(101);
        mysqlDriver.setDriverAvatar("未开通");


        QueryWrapper<CarPojo> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("car_driverno",driverPojo.getCarDriverno());
        List<CarPojo> carPojos = carPojoMapper.selectList(queryWrapper4);
        if (carPojos.size()>0) {
            return new BaseDto(206,"驾驶证被用了",null);
        }

        int insert = driverPojoMapper.insert(mysqlDriver);

        if (insert==0){
           return new BaseDto(301,"写数据库失败，请检查",null);
        }
        QueryWrapper<DriverPojo> driverPojoQueryWrapper = new QueryWrapper<>();
        driverPojoQueryWrapper.eq("driver_idno",driverPojo.getDriverIdno());
        DriverPojo driverPojo7 = driverPojoMapper.selectOne(queryWrapper3);



        CarPojo carPojo = new CarPojo();
        carPojo.setCarBrand(driverPojo.getCarBrand());
        carPojo.setCarColor(driverPojo.getCarColor());
        carPojo.setCarPlateno(driverPojo.getCarPlateno());
        carPojo.setCarDriverno(driverPojo.getCarDriverno());
        carPojo.setCarDrivernoimg("driverno"+driverPojo.getDriverIdno());
        carPojo.setDriverId(driverPojo7.getDriverId());
        carPojo.setCarImg("car"+driverPojo.getDriverIdno());
        carPojo.setCarRunproveimg("run"+driverPojo.getDriverIdno());

        int insert1 = carPojoMapper.insert(carPojo);



        return new BaseDto(101,"提交审核了已，请耐心等候",null);

    }

    @Override
    public BaseDto fixPwd(DriverPojo driverPojo) {
        System.err.println(driverPojo);
        QueryWrapper<DriverPojo> driverPojoQueryWrapper = new QueryWrapper<>();
        driverPojoQueryWrapper.eq("driver_acc",driverPojo.getDriverAcc());
        driverPojoQueryWrapper.eq("driver_pwd",driverPojo.getDriverName());
        List<DriverPojo> driverPojos = driverPojoMapper.selectList(driverPojoQueryWrapper);
        if (driverPojos.size()==0){
            return new BaseDto(201,"你知道我想说什么，数据库无返回",null);
        }

        UpdateWrapper<DriverPojo> driverUw = new UpdateWrapper<>();
        driverUw.eq("driver_acc",driverPojo.getDriverAcc()).set("driver_pwd",driverPojo.getDriverPwd());
        int update = driverPojoMapper.update(null, driverUw);
        if (update==1){
            return new BaseDto(101,"成功修改",null);
        }



        return new BaseDto(102,"修改失败，可能是与原密码相同",null);
    }


}




